using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using System.IO;

using SypGui.ConnectionUC;
using SypConst.Garbage;
using SypGui.CreateForms;

using SypConst.Path;
using SypEngine;

namespace SypGui.CreateForms
{

    public partial class SypContainerForm : SypFormDesign
    {
        private int SiteId = -1;
        
        private int currentStep = 0;

        private List<SypUserControl> ListStepUC = null;



        public void initializeSypUC(SypForm SypForm)
        {
            Text = GetRessources("WizardName");
            ListStepUC = new List<SypUserControl>();
            SypForm.evt_onLOCAL_DONE += new VisitorCallBack(FormClosingAfterProcess);

            this.SuspendLayout();
            CreationFirstStep SypFormStep1 = new CreationFirstStep(OutputMethod);
            SypFormStep1.Location = new System.Drawing.Point(-4, 0);
            SypFormStep1.Name = "creationFirstStep1";
            SypFormStep1.Size = new System.Drawing.Size(399, 222);
            SypFormStep1.TabIndex = 0;
            this.Controls.Add(SypFormStep1);
            ListStepUC.Add(SypFormStep1);
            SypFormStep1.Show();

            ConnectionStep2 SypFormStep2 = new ConnectionStep2(OutputMethod);
            this.Controls.Add(SypFormStep2);
            ListStepUC.Add(SypFormStep2);
            SypFormStep2.Hide();

            ProcessStep3 SypFormStep3 = new ProcessStep3(OutputMethod, SypForm);
            this.Controls.Add(SypFormStep3);
            ListStepUC.Add(SypFormStep3);
            SypFormStep3.Hide();
            this.ResumeLayout(false);

            this.CenterToScreen();

        }

        public SypContainerForm() : base() { }

        public SypContainerForm(SetText OutputMethod, SypForm SypForm)
            : base (OutputMethod)
        {
            SiteId = SypGarbageConf.Instance().GetCurrentId();
            initializeSypUC(SypForm);
        }

        private void HideExcept(int UCIndex)
        {
            for(int i = 0; i < ListStepUC.Count; i++)
            {
                if (i != UCIndex)
                    ListStepUC[i].Hide();
                else
                    ListStepUC[UCIndex].Show();
            }
        }

        public void UpdateSiteId()
        {

            foreach (SypUserControl SypUC in ListStepUC)
            {
                lock (SypUC)
                {
                    if (SypUC is ConnectionStep2)
                        ((ConnectionStep2)SypUC).UpdateSiteId();
                    else
                        SypUC.SiteId = SypGarbageConf.Instance().GetCurrentId();
                }
            }
            this.SiteId = SypGarbageConf.Instance().GetCurrentId();
        }


        private void ResetWizard(bool clearMode)
        {
            currentStep = 0;
            foreach (SypUserControl SypUC in ListStepUC)
            {
                SypUC.Hide();
                SypUC.Reset();
            }
            ListStepUC[currentStep].Show();

            ClientSize = new System.Drawing.Size(399, 222);
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;

            if (clearMode)
            {
                string cmd = "clearfull-synchronise"  + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR + 42;
               OutputMethod(cmd, Output.OUT);
               OutputMethod(cmd, Output.COMMAND);

                //string cmd2 = "online-synchronise" + SypGarbage.COMMAND_SEPARATOR + SiteId;
                //OutputMethod(cmd2, Output.OUT);
                //OutputMethod(cmd2, Output.COMMAND);
            }
        }


        public void GetNextStepUC(bool update)
        {
            this.SuspendLayout();
            if (update)
                UpdateSiteId();
            if ((currentStep + 1) > (ListStepUC.Count - 1))
                currentStep = (ListStepUC.Count - 1);
            else
                currentStep += 1;


            if (ListStepUC[currentStep] != null)
            {
                UserControl currentForm = ListStepUC[currentStep];
                HideExcept(currentStep);
            }
            this.ResumeLayout(false);
        }

        public void GetPreviousStepUC()
        {
            this.SuspendLayout();
            //UpdateSiteId();
            if ((currentStep - 1) < 0)
                currentStep = 0;
            else
                currentStep -= 1;

            if (ListStepUC[currentStep] != null)
            {
                UserControl currentForm = ListStepUC[currentStep];
                HideExcept(currentStep);
            }
            this.ResumeLayout(false);
        }


        #region Events


        private void FormClosingAfterProcess(int SiteId)
        {
            Hide();
            ResetWizard(false);
            //string indexpage = SypGarbage.TMP_FOLDER + SiteId + "/" + SypGarbage.LOCAL_FOLDER + SypGarbage.DEFAULT_PAGE;
            string indexpage = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.INDEX_PAGE, null, SiteId);
            if (File.Exists(indexpage))
            {
                System.Diagnostics.Process p = null;
                try
                {
                    p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = SypGarbageConf.Instance().SypDefaultBrowser;
                    p.StartInfo.Arguments = "\"" + Application.StartupPath + '\\' + indexpage.Replace('/', '\\') + "\"";
                    p.Start();
                }
                catch (Exception)
                {
                    OutputMethod("Failed to start default browser (" + p.StartInfo.FileName + ") at path :" + p.StartInfo.Arguments, Output.ERROR);
                }
                finally
                {
                    p.Close();
                }
            }
        }

        private  void SypContainerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.timeToClose)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
            ResetWizard(true);
            Hide();
        }

        #endregion
    }
}